package com.lzh.shijicloudstack.template;

import cn.hutool.core.collection.CollectionUtil;
import com.lzh.shijicloudstack.model.PageModel;
import com.lzh.shijicloudstack.utils.MyTools;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页查询模板
 *
 * @author luzhiheng
 * @date 2024-08-27
 */
public abstract class PageAbstractTemplate {

    /**
     * 查询条件
     */
    public Map<String, Object> queryMap;

    /**
     * 分页参数
     */
    public PageModel pageModel;

    /**
     * 添加公共查询条件
     *
     * @param pageModel 分页对象
     */
    public void addCommonQuery(PageModel pageModel) {
        String queryField = null;
        if (MyTools.checkMapStringObjectNullOrNot(pageModel.getQuery())) {
            queryField = MyTools.changeQueryMapIntoStringStoreList(pageModel.getQuery());
        }
        //获取当前的总记录条数
        this.queryMap = new HashMap<>();
        this.queryMap.put("queryField", queryField);
        this.pageModel = pageModel;
    }

    /**
     * 添加自定义查询条件
     *
     * @param customQueryMap 自定义查询条件
     */
    public void addCustomQuery(Map<String, Object> customQueryMap) {
        if (!ObjectUtils.isEmpty(customQueryMap)) {
            this.queryMap.putAll(customQueryMap);
        }
    }

    /**
     * 获取总记录数查询条件
     *
     * @param pageModel      分页对象
     * @param customQueryMap 自定义查询条件
     * @return 总记录数查询条件
     */
    public Map<String, Object> getQueryMap(PageModel pageModel, Map<String, Object> customQueryMap) {
        addCommonQuery(pageModel);
        addCustomQuery(customQueryMap);
        return this.queryMap;
    }

    /**
     * 获取总记录数
     *
     * @return 总记录数
     */
    public abstract Integer countTotal();

    public void dealWithPageModel(PageModel pageModel) {
        //处理基本pageModel
        MyTools.dealWithPageModel(pageModel);

        //查询对象
        Map<String, Object> queryTotalMap = MyTools.createQueryMapForPageModel(pageModel, null);
        queryTotalMap.putAll(this.queryMap);
        this.queryMap = queryTotalMap;
    }

    /**
     * 获取该页数据
     *
     * @return 结果
     */
    public abstract List<Map<String, Object>> queryPageList();


    /**
     * 分页查询
     *
     * @return 结果
     */
    public Map<String, Object> queryPage() {
        // 查询总记录数
        Integer total = countTotal();
        if (total == null || total == 0) {
            //如果没有查询到的数据，那么需要返回null
            return returnNullMap();
        }
        this.pageModel.setTotal(total);
        // 处理分页
        dealWithPageModel(this.pageModel);
        // 获取该页数据
        List<Map<String, Object>> pageList = queryPageList();
        // 封装结果
        if (CollectionUtil.isNotEmpty(pageList)) {
            this.pageModel.setDataList(pageList);
            Map<String, Object> result = new HashMap<>(1);
            result.put("list", this.pageModel);
            return result;
        }
        return returnNullMap();
    }


    /**
     * 返回空记录
     *
     * @return 空记录
     */
    public Map<String, Object> returnNullMap() {
        Map<String, Object> result = new HashMap<>(1);
        PageModel pageModel = new PageModel();
        result.put("list", pageModel);
        pageModel.setDataList(new ArrayList<>());
        return result;
    }
}
